package dynamic.adapter.base;

import dynamic.ibatis.session.DConfiguration;
import dynamic.metadata.DOrderFieldInfo;
import dynamic.metadata.DTableFieldInfo;
import dynamic.metadata.DTableInfo;
import dynamic.metadata.EntityProperty;
import org.apache.ibatis.session.Configuration;

import java.lang.reflect.Field;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public interface DITableInfoAdapter {

    default boolean mpjHasLogic(DTableInfo tableInfo) {
        return tableInfo.isWithLogicDelete();
    }

    default boolean mpjIsPrimitive(DTableFieldInfo tableFieldInfo) {
        return tableFieldInfo.isPrimitive();
    }

    default DTableFieldInfo mpjGetLogicField(DTableInfo tableInfo) {
        return tableInfo.getLogicDeleteFieldInfo();
    }

    default boolean mpjHasPK(DTableInfo tableInfo) {
        return tableInfo.havePK();
    }

    default DConfiguration mpjGetConfiguration(DTableInfo tableInfo) {
        return tableInfo.getConfiguration();
    }

    default EntityProperty mpjGetField(DTableFieldInfo fieldInfo, Supplier<EntityProperty> supplier) {
        return fieldInfo.getField();
    }

    default List<DOrderFieldInfo> mpjGetOrderField(DTableInfo tableInfo) {
        return tableInfo.getOrderByFields().stream().map(f ->
                new DOrderFieldInfo(f.getColumn(), f.getType(), f.getSort())).collect(Collectors.toList());
    }
}
